const { proxy, fuseListSelect, canSave, baseRequest, renderFuseList } = window._videoFuse;

const save = async () => {
    const $form = this.pageObj.getApiWidgetByAliasName('fuse_modal_form').widgetObj;
    const formData = $form.getFormData('fuse_modal_form');
    if (!formData.fuseName || !formData.projectCode) return;
    const $videoSwiper = this.pageObj.getApiWidgetByAliasName('fuse_modal_video_swiper').widgetObj;
    const playerData = $videoSwiper.sullivan.getData();

    const videoFuseDetails = (playerData || []).map(item => ({
        equipmentInfo: JSON.stringify(item),
        equipmentId: item.equipmentCode,
    }));

    if (fuseListSelect && fuseListSelect.operate === 'edit') {
        const body = { ...formData, id: fuseListSelect.id, videoFuseDetails };
        await baseRequest(`/${proxy}SummaryAssessmentSV/videoFuse/update`, body, 'POST');
    } else {
        const body = { ...formData, videoFuseDetails };
        await baseRequest(`/${proxy}SummaryAssessmentSV/videoFuse/insert`, body, 'POST');
    }
    renderFuseList();
    const modals = this.pageObj.getApiWidgetsByClassName('fuse-modal');
    modals.forEach(v => v.hideWidget());
    $videoSwiper.sullivan.closeAll();
    const $fuseVideo = this.pageObj.getApiWidgetByAliasName('fuse_modal_video');
    $fuseVideo.widgetObj.sullivan.closeAll();
}

if (canSave) {
    _videoFuse.videoAreaSearch = { id: '', name: '' };
    const $form = this.pageObj.getApiWidgetByAliasName('video_search_form').widgetObj;
    $form.resetForm('video_search_form');
    save();
} else {
    layer.msg('当前视频成功融合后, 才可进行保存');
}


// const $table = this.pageObj.getApiWidgetByAliasName('turn_monitor_table').widgetObj;
// const $roundTable = this.pageObj.getApiWidgetByAliasName('round_robin_table').widgetObj;
// const dataSource = [];
// const map = new Map();
// ($roundTable.tbodyData || []).forEach(item => {
//     map.set(item.id, true);
//     dataSource.push({ ...item, LAY_CHECKED: false, presetNum: Number(item.presetNum) });
// });
// ($table.table?.getCheckedRow() || []).forEach(item => {
//     if (!map.has(item.id)) {
//         map.set(item.id, true);
//         const equipmentPresets = (item.equipmentPresets || []).map(preset => ({
//             ...preset,
//             name: preset.presetPointName,
//             value: preset.presetPointIndex,
//         }));
//         const presetNum = equipmentPresets[0].presetPointIndex;
//         dataSource.push({ ...item, LAY_CHECKED: false, equipmentPresets, presetNum });
//     }
// });
// console.log($roundTable.tbodyData, dataSource);

// $roundTable.setDataSource(dataSource);
// const turnModals = this.pageObj.getApiWidgetsByClassName('turn-monitor');
// turnModals.forEach(v => v.hideWidget());
// const modals = this.pageObj.getApiWidgetsByClassName('round-robin-modal');
// modals.forEach(v => v.showWidget());


function insertFuse (fuseDetails) {
    const videoFuseDetails = fuseDetails.map(item => ({
        equipmentInfo: JSON.stringify(item),
        equipmentId: item.equipmentCode,
    }));

}